/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

// File: logTest.cxx
// Author: Cory Dixon
// Date: 2/27/01
//
// Program to test the serial port both in Unix
// and VxWorks
//

#include <unistd.h>
#include <signal.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>

#include "String.h"
#include "timeLib.h"
#include "fileLog.h"
#include "queue.h"
#include "matrix.h"


#define NUM_TIME_LOOP            1024  // number of time to send time loop
#define TIME_LOOP_FILE 			"log/linuxTime.dat"

//----------------------------------------------------------
// Main function
//----------------------------------------------------------

int main(int argc, char *argv[])
{
	String base = "log/linuxTime";

	// used for getopt to get otions from arguments
	extern int optind;
	//extern char * optarg;

	if( argc ==  2) {
		base = String(argv[optind]); 
	} 

	FileLog log(base.c_str(), FileLog::CREATE);
	Matrix timeQueue(NUM_TIME_LOOP-1,1,0.0);

	// open log
	cout << "Creating log file" << endl;
	if( ! log.open() ) {
		cout << "Could not open " << log.getFileName() << endl;
		return ERROR;
	}

	for( int i=0; i < NUM_TIME_LOOP; i++ )
	{ 
		cout << i << endl;
		if( log.write("0123456789", 10) != 10)
			cout << "Did not write buffer to log" << endl;
		usleep(1);
	}

	if( ! log.close() ){
		cout << "Could not close log" << endl;
		return ERROR;
	}	
	cout << "--------------------" << endl
		<< "Bytes Written: " << log.bytesWritten() 
		<< "\tFile Size: " << log.fileSize() << endl;


	cout << "Reading log file" << endl;

	// now read back the lines
	FileLog logR(base.c_str(), FileLog::READ);
	LogFileStruct line;
	int linesRead = 0;
	if( ! logR.open() ) {
		cout << "Could not open " << logR.getFileName() << endl;
		return ERROR;
	}

	logR.read( line );
	linesRead++;

	struct tm tmTime = logR.getTime();
	cout << "--------------------" << endl
		<< "File Time:"
		<< " year=" << tmTime.tm_year + 1900
		<< " month=" << tmTime.tm_mon + 1
		<< " day=" << tmTime.tm_mday 
		<< " hour=" << tmTime.tm_hour 
		<< " min=" << tmTime.tm_min 
		<< " sec=" << tmTime.tm_sec << endl;

	float lastTime = line.timeOfDay;
	float diffTime = 0;
	while( logR.read( line ) > 0 ) {
		diffTime = line.timeOfDay - lastTime;
		lastTime = line.timeOfDay;

		// save command ping
		//timeQueue.Push( (QueueType *)&(diffTime), 1 ); 
		timeQueue(linesRead - 1,0) = diffTime;

		linesRead++;
	}
	timeQueue.saveToFile( TIME_LOOP_FILE );

	cout << "Number of lines: " << linesRead << endl;
	cout << "--------------------" << endl
		<< " Mean=" << timeQueue.Mean() 
		<< " Std=" << timeQueue.Std() 
		<< " Min=" << timeQueue.Min() 
		<< " Max=" << timeQueue.Max()
		<< endl;

	logR.close();

	// exit
	return OK;
}
